package com.teensoft.core.user.imp;

import java.util.List;

import com.teensoft.base.dao.imp.HibernateDAO;
import com.teensoft.base.exception.DAOException;
import com.teensoft.core.user.Organization;
import com.teensoft.core.user.OrganizationDAO;
import com.teensoft.core.user.common.Utils;

public class OrganizationDAOImp extends HibernateDAO implements OrganizationDAO {

	public void addOrganization(Organization o) throws DAOException {
		this.saveOrUpdate(o);
	}

	public void deleteOrganizationById(String id) throws DAOException {
		this.delete(Organization.class, id);
	}

	public Organization getById(String id) throws DAOException {
		return (Organization)this.get(Organization.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Organization> getChild(String id) throws DAOException {
		String hql = "from parent = '"+id+"'";
		List<Organization> result = this.getList(hql);
		return result;
	}

	public Organization getParent(String id) throws DAOException {
		String pid = this.getById(id).getParent();
		if(!Utils.isNull(pid)){
			return this.getById(pid);
		}
		return null;
	}

	public void updateOrganization(Organization o) throws DAOException {
		this.saveOrUpdate(o);
	}

}
